//
//  MenuHomeWebsiteView.m
//  BaoVn
//
//  Created by Le Anh Tai on 11/16/14.
//  Copyright (c) 2014 Le Anh Tai. All rights reserved.
//

#import "MenuHomeWebsiteView.h"

@implementation MenuHomeWebsiteView

- (instancetype)init
{
    if (self = [super init])
    {
        _alphaView = [[UIView alloc] init];
        _alphaView.userInteractionEnabled = YES;
        _alphaView.backgroundColor = [UIColor blackColor];
        [_alphaView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)]];
        [self addSubview:_alphaView];
        
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_contentView];
        
        _listItemButton = [[UIButton alloc] init];
        [_listItemButton setTitle:@"List" forState:UIControlStateNormal];
        [_listItemButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [_listItemButton setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
        [_listItemButton addTarget:self action:@selector(listItemPressed) forControlEvents:UIControlEventTouchUpInside];
        [_contentView addSubview:_listItemButton];
        
        _cardItemButton = [[UIButton alloc] init];
        [_cardItemButton setTitle:@"Card" forState:UIControlStateNormal];
        [_cardItemButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [_cardItemButton setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
        [_cardItemButton addTarget:self action:@selector(cardItemPressed) forControlEvents:UIControlEventTouchUpInside];
        [_contentView addSubview:_cardItemButton];
        
        self.hidden = YES;
    }
    
    return self;
}

- (void)setSelectedNewsTypeButton:(UIButton *)button
{
    _listItemButton.selected = NO;
    _cardItemButton.selected = NO;
    
    button.selected = YES;
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    
    NSInteger marginTop = 44;
    NSInteger widMenu = 200;
    NSInteger heiMenu = 300;
    NSInteger heiButton = 44;
    
    if (IS_SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_IOS7)
    {
        marginTop += 20;
    }
    
    _alphaView.frame = self.bounds;
    _contentView.frame = RECT(_contentView.left, marginTop, widMenu, heiMenu);
    _listItemButton.frame = RECT(0, 0, _contentView.width/2, heiButton);
    _cardItemButton.frame = RECT(_listItemButton.right, 0, _contentView.width - _listItemButton.right, heiButton);
}

- (void)animateShowMenu
{
    if ([AppInfo shared].newsType == NewsTypeItem)
    {
        [self setSelectedNewsTypeButton:_listItemButton];
    }
    else
    {
        [self setSelectedNewsTypeButton:_cardItemButton];
    }
    
    _alphaView.alpha = 0;
    _contentView.left = self.width;
    self.hidden = NO;
    
    [UIView animateWithDuration:0.25 animations:^{
        _alphaView.alpha = 0.6f;
        _contentView.right = self.width;
    }];
}

- (void)animateHideMenu
{
    [UIView animateWithDuration:0.25 animations:^{
        _alphaView.alpha = 0;
        _contentView.left = self.width;
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}

#pragma mark -
- (void)listItemPressed
{
    [self animateHideMenu];
    
    if ([_delegate respondsToSelector:@selector(menuHomeWebsiteDidPressListNewsItem:)])
    {
        [_delegate menuHomeWebsiteDidPressListNewsItem:self];
    }
}

- (void)cardItemPressed
{
    [self animateHideMenu];
    
    if ([_delegate respondsToSelector:@selector(menuHomeWebsiteDidPressCardNewsItem:)])
    {
        [_delegate menuHomeWebsiteDidPressCardNewsItem:self];
    }
}

- (void)gestureHandler:(id)sender
{
    [self animateHideMenu];
}

@end
